// 获取新的token
const { util, InsideErr } = require("utils");
const { proxyGetRequest, getRequest } = require("request");
const { GetWxWebTokenURL } = require("./api.js");
const { UNiCloudProvider } = require("../../constEnum.js");
async function getNewToken(appId, appSecret, provider) {
    let apiTool = provider === UNiCloudProvider.alipay ? getRequest : proxyGetRequest;
    // 判断是不是 支付宝小程序云
    // 阿里云使用  代理
    let [err, data] = await apiTool(GetWxWebTokenURL)({
        grant_type: "client_credential",
        appid: appId,
        secret: appSecret,
    });
    if (!util.nilValue(err)) {
        return [err, null];
    }
    let resData = data || {};
    if (!util.nilValue(resData.errcode) && resData.errcode !== 0) {
        let err = new InsideErr(resData);
        return [err, null];
    } else {
        return [null, resData];
    }
}

module.exports = {
    getNewToken,
};
